<?php  
class ControllerSaleCouponRule extends Controller {
	private $error = array();
     
  	public function index() {
		$this->load->language('sale/coupon_rule');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('sale/coupon_rule');
		$this->getList();
  	}
  
  	public function insert() {
    	$this->load->language('sale/coupon_rule');
    	$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('sale/coupon_rule');
    	if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
			$this->model_sale_coupon_rule->addCouponRule($this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('sale/coupon_rule', 'token=' . $this->session->data['token'], 'SSL'));
    	}
    	$this->getForm_text();
    	$this->getForm_insert();
  	}

  	public function update() {
    	$this->load->language('sale/coupon_rule');
    	$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('sale/coupon_rule');
    	if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
			$this->model_sale_coupon_rule->editCouponRule($this->request->get['coupon_rule_id'], $this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('sale/coupon_rule', 'token=' . $this->session->data['token'], 'SSL'));
		}
    	$this->getForm_text();
    	$this->getForm_update();
  	}

  	private function getList() {
  		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('sale/coupon_rule', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
   		
   		$this->data['insert'] = $this->url->link('sale/coupon_rule/insert', 'token=' . $this->session->data['token'], 'SSL');
   		
   		$url = '';
  		if (isset($this->request->get['filter_display_name'])) {
			$filter_display_name = $this->request->get['filter_display_name'];
			$url .= '&filter_display_name=' . $this->request->get['filter_display_name'];
		} else {
			$filter_display_name = null;
		}
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
		
		$data = array(
			'filter_display_name'  => $filter_display_name,
			'start' => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit' => $this->config->get('config_admin_limit')
		);
		$coupon_rule_total = $this->model_sale_coupon_rule->getTotalCouponRules($data);
		$results = $this->model_sale_coupon_rule->getCouponRules($data);
 		$this->data['coupon_rules'] = array();
    	foreach ($results as $result) {
			$action = array();
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('sale/coupon_rule/update', 'token=' . $this->session->data['token'] . '&coupon_rule_id=' . $result['coupon_rule_id'], 'SSL')
			);
						
			$this->data['coupon_rules'][] = array(
				'coupon_rule_id'      => $result['coupon_rule_id'],
				'name'                => $result['name'],
				'display_name'        => $result['display_name'],
				'discount_type'       => $result['discount_type'],
				'discount_points'     => $result['discount_points'],
				'discount_values'     => $result['discount_values'],
				'max_use_time'        => $result['max_use_time'],
				'max_use_day'         => $result['max_use_day'],
				'category_ids'        => $result['category_ids'],
				'action'              => $action
			);
		}
		$this->data['filter_display_name'] = $filter_display_name;
		$this->data['token'] = $this->session->data['token'];
									
		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_no_results'] = $this->language->get('text_no_results');
		$this->data['text_discount_type_0'] = $this->language->get('text_discount_type_0');
		$this->data['text_discount_type_1'] = $this->language->get('text_discount_type_1');
		$this->data['text_discount_type_2'] = $this->language->get('text_discount_type_2');

		$this->data['column_name'] = $this->language->get('column_name');
		$this->data['column_display_name'] = $this->language->get('column_display_name');
		$this->data['column_discount_type'] = $this->language->get('column_discount_type');
		$this->data['column_discount_points'] = $this->language->get('column_discount_points');
		$this->data['column_discount_values'] = $this->language->get('column_discount_values');
		$this->data['column_max_use_time'] = $this->language->get('column_max_use_time');
		$this->data['column_max_use_day'] = $this->language->get('column_max_use_day');
		$this->data['column_category_ids'] = $this->language->get('column_category_ids');
		$this->data['column_action'] = $this->language->get('column_action');
		
		$this->data['button_insert'] = $this->language->get('button_insert');
		$this->data['button_filter'] = $this->language->get('button_filter');
 
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$pagination = new Pagination();
		$pagination->total = $coupon_rule_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = HTTPS_SERVER . 'index.php?route=sale/coupon_rule&token=' . $this->session->data['token'] . $url . '&page={page}';
			
		$this->data['pagination'] = $pagination->render();

		$this->template = 'sale/coupon_rule_list.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render());
  	}

  	private function getForm_text() {
    	$this->data['heading_title'] = $this->language->get('heading_title');
		
    	$this->data['text_discount_type_0'] = $this->language->get('text_discount_type_0');
		$this->data['text_discount_type_1'] = $this->language->get('text_discount_type_1');
		$this->data['text_discount_type_2'] = $this->language->get('text_discount_type_2');
		
		$this->data['entry_name'] = $this->language->get('entry_name');
		$this->data['entry_display_name'] = $this->language->get('entry_display_name');
    	$this->data['entry_description'] = $this->language->get('entry_description');
		$this->data['entry_discount_type'] = $this->language->get('entry_discount_type');
		$this->data['entry_discount_points'] = $this->language->get('entry_discount_points');
		$this->data['entry_discount_values'] = $this->language->get('entry_discount_values');
		$this->data['entry_max_use_time'] = $this->language->get('entry_max_use_time');
		$this->data['entry_max_use_day'] = $this->language->get('entry_max_use_day');
		$this->data['entry_category_ids'] = $this->language->get('entry_category_ids');
		
    	$this->data['button_save'] = $this->language->get('button_save');
    	$this->data['button_cancel'] = $this->language->get('button_cancel');

		$this->data['token'] = $this->session->data['token'];
	
  		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		
  		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('sale/coupon_rule', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);

   		$this->load->model('localisation/language');
		$this->data['languages'] = $this->model_localisation_language->getLanguages();
		
		$this->load->model('product/category');
		$this->data['categories'] = $this->model_product_category->getAllCategoriesForSelect(0, '', '', -1, 0);
		
  	}
	
	private function getForm_insert() {
		$this->data['action'] = $this->url->link('sale/coupon_rule/insert', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['cancel'] = $this->url->link('sale/coupon_rule', 'token=' . $this->session->data['token'], 'SSL');
  		
		$this->data['name'] = '';
      	$this->data['discount_type'] = 0;
      	$this->data['discount_points'] = '';
      	$this->data['discount_values'] = '';
		$this->data['max_use_time'] = 3;
		$this->data['max_use_day'] = 90;
      	$this->data['category_ids'] = '';
      	$this->data['coupon_rule_description'] = array();
		
		$this->template = 'sale/coupon_rule_form.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render());		
  	}

	private function getForm_update() {
    	$this->data['action'] = $this->url->link('sale/coupon_rule/update', 'token=' . $this->session->data['token'] . '&coupon_rule_id=' . $this->request->get['coupon_rule_id'], 'SSL');
		$this->data['cancel'] = $this->url->link('sale/coupon_rule', 'token=' . $this->session->data['token'], 'SSL');
  		
		$coupon_rule_info = $this->model_sale_coupon_rule->getCouponRule($this->request->get['coupon_rule_id']);
    	$this->data['name'] = $coupon_rule_info['name'];
		$this->data['discount_type'] = $coupon_rule_info['discount_type'];
		$this->data['discount_points'] = $coupon_rule_info['discount_points'];
		$this->data['discount_values'] = $coupon_rule_info['discount_values'];
		$this->data['max_use_time'] = $coupon_rule_info['max_use_time'];
		$this->data['max_use_day'] = $coupon_rule_info['max_use_day'];
		$this->data['category_ids'] = $coupon_rule_info['category_ids'];
		
		$this->data['coupon_rule_description'] = $this->model_sale_coupon_rule->getCouponRuleDescription($this->request->get['coupon_rule_id']);
 
		$this->template = 'sale/coupon_rule_form.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render());		
  	}	
	
	public function autocompleteCouponRule() {
		$json = array();
		if (isset($this->request->get['filter_display_name'])) {
			$this->load->model('sale/coupon_rule');
			$json = $this->model_sale_coupon_rule->autocompleteCouponRule($this->request->get['filter_display_name']);
		}
		$this->response->setOutput(json_encode($json));
	}
}
?>